/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.core.Cloneable;
import fever.structures.color.HSLRange;
import fever.structures.IColor;
import fever.utils.ColorSpace;
import fever.utils.Stringifier;

/**
 * Defines color using HSL componants and range.
 * 
 * <p>You have to define your HSL Range before any HSL instanciation, take a 
 * lokk at example below.
 * 
 * <p>
 * {@code
 *   ColorSpace.HSLRANGE = HSLRange.WINDOWS;
 *   
 *   var color : HSL = new HSL( 100, 100, 100 );
 * }
 * 
 * @see fever.structures.color.HSLRange
 * @see fever.structures.IColor
 * @see fever.utils.ColorSpace
 * 
 * @author Romain Ecarnot
 */
class fever.structures.color.HSL implements IColor, Cloneable
{
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private var _hue : Number;
	private var _saturation : Number;
	private var _luminance : Number;
	private var _range : HSLRange;
	 	 
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Hue value. */
	public function set hue( v : Number ) { _setHue( v ); }
	public function get hue() : Number { return _hue; }
	
	/** Saturation value. */
	public function set saturation( v : Number ) { _setSaturation( v ); }
	public function get saturation() : Number { return _saturation; }
	
	/** Luminance value. */
	public function set luminance( v : Number ) { _setLum( v ); }
	public function get luminance() : Number { return _luminance; }
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function HSL( h : Number, s : Number, l : Number )
	{
		_range = ColorSpace.HSLRANGE;
		
		_setHue( h );
		_setSaturation( s );
		_setLum( l );
	}
	
	/** 
	 * Returns color value in Number format.
	 */
	public function getColor() : Number
	{
		return ColorSpace.hslToHexa( this );
	}
	
	/**
	 * Return color value in hexa String format.
	 */ 
	public function getString() : String
	{
		return ColorSpace.hslToString( this );
	}
	
	/**
	 * Returns a clone instance.
	 * 
	 * @return Cloneable
	 */
	public function clone() : Cloneable
	{
		return new HSL( _hue, _saturation, _luminance );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this, "HSL [" + _hue + "." + _saturation + "." + _luminance + "]" );	
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
		
	private function _setHue(v : Number) : Void
	{
		if( v >= _range.hue.min && v <= _range.hue.max ) _hue = v;
		else _hue = 0;
	}

	private function _setSaturation(v : Number) : Void
	{
		if( v >= _range.saturation.min && v <= _range.saturation.max ) _saturation = v;
		else _saturation = 0;
	}

	private function _setLum(v : Number) : Void
	{
		if( v >= _range.luminance.min && v <= _range.luminance.max ) _luminance = v;
		else _luminance = 0;
	}
}